package com.foxzzz.binary.search;

public class Solution4 {
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int[] longArray = null;
        int[] shortArray = null;
        if (nums1.length > nums2.length) {
            longArray = nums1;
            shortArray = nums2;
        } else {
            longArray = nums2;
            shortArray = nums1;
        }
        int max=longArray.length+shortArray.length;
        double resultCulsor = max / 2.0;
        int[] sumArray=new int[max];

        for (int i = 0,j = 0,k = 0;k<max; k++) {
            if(i<longArray.length&&j<shortArray.length) {
                if (longArray[i] < shortArray[j]) {
                    sumArray[k] = longArray[i];
                    i++;
                } else {

                        sumArray[k] = shortArray[j];
                        j++;

                }
            }
            else if(j == shortArray.length){
                    sumArray[k] = longArray[i];
                    i++;
                }
            else if(i == longArray.length){
                    sumArray[k] = shortArray[j];
                    j++;
                }
        }
        if(((int) resultCulsor)!=resultCulsor){
            return sumArray[(int) resultCulsor];
        }else {
            return (sumArray[(int)resultCulsor]+sumArray[(int)resultCulsor-1])/2.0;
        }

    }

    public static void main(String[] args) {
        System.out.println(new Solution4().findMedianSortedArrays(new int[]{1,2},new int[]{3,4}));
    }
}
